/**
 * Filename: MemberActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/6 14:45
 */
package com.github.postsanf.yinian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import org.json.JSONObject;

import java.util.LinkedHashMap;

public class PuzzleSelectActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout play_num;
    int[] picNum = {R.drawable.picture_num0,
            R.drawable.picture_num1,
            R.drawable.picture_num2,
            R.drawable.picture_num3,
            R.drawable.picture_num4,
            R.drawable.picture_num5,
            R.drawable.picture_num6,
            R.drawable.picture_num7,
            R.drawable.picture_num8,
            R.drawable.picture_num9
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_puzzleselect);
        initView();
        getGamePeoples();
    }

    private void initView() {
        findViewById(R.id.puzzle_select_back).setOnClickListener(this);
        findViewById(R.id.puzzle_try).setOnClickListener(this);
        findViewById(R.id.puzzle_do).setOnClickListener(this);
        play_num = (LinearLayout) findViewById(R.id.play_num);

    }

    private void getGamePeoples() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        StringRequest req = new StringRequest(getString(R.string.ynGetPuzzleGameTotalNum))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        try {
                            JSONObject json = new JSONObject(s);
                            if (ReqUtils.isSuccess(json.optString("code"))) {
                                String peoplenum = json.optJSONArray("data").optJSONObject(0).optString("number");
                                for (int i = 0; i < peoplenum.length(); i++) {
                                    ImageView iv = new ImageView(PuzzleSelectActivity.this);
                                    iv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                                    char index = peoplenum.charAt(i);
                                    int ii = Integer.parseInt(String.valueOf(index));
                                    iv.setImageResource(picNum[ii]);
                                    play_num.addView(iv);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        Toast.makeText(PuzzleSelectActivity.this, CommonConstants.YN_NET_LINK, Toast.LENGTH_SHORT).show();
                    }
                });
        liteHttp.executeAsync(req);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.puzzle_select_back:
                finish();
                break;
            case R.id.puzzle_try:
                startActivity(new Intent(PuzzleSelectActivity.this, PuzzleWebActivity.class));
                break;
            case R.id.puzzle_do:
                startActivity(new Intent(PuzzleSelectActivity.this, LocalPuzzleManageActivity.class));
                break;
        }
    }
}